package nl.xs4all.dylanschell.deckprinter.model

import java.util.UUID
import java.io.File
import collection.mutable.HashMap
import nl.xs4all.dylanschell.deckprinter.util.{SDistance, Distance}

@serializable
case class SSetDescriptor(sourcefile: File, id: UUID, name: String) {

  val cards = new HashMap[UUID, CardInSet]
  val images = new HashMap[UUID, String]

  def getCardById(id: UUID): CardInSet = cards(id)

  def getImage(card: CardInSet): String = images(card.getId)

  def registerImage(uuid: UUID, target: String): Unit = images(uuid) = target;

  def getCard(card: SCardInDeck): CardInSet = cards(card.id)

  def getCardByName(name: String, fuzzy: Boolean = false): CardInSet = {
    var minDistance = Integer.MAX_VALUE
    var closestCard: CardInSet = null
    for (cis <- cards.values) {
      var d = SDistance.distance(name, cis.getName)
      if (d < minDistance) {
        minDistance = d
        if (d == 0) {
          return cis;
        }
        closestCard = cis
      }
    }
    return closestCard
  }
}